package com.zt.location;
 
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.zt.location.common.utils.SysCache;
import com.zt.location.modules.base.entity.HtReadersEntity;
import com.zt.location.modules.base.entity.LimitspeedEntity;
import com.zt.location.modules.base.entity.RealTimeEntity;
import com.zt.location.modules.base.entity.RfidinfoEntity;
import com.zt.location.modules.base.service.HtReadersService;
import com.zt.location.modules.base.service.LimitspeedService;
import com.zt.location.modules.base.service.RealTimeService;
import com.zt.location.modules.base.service.RfidinfoService;
import com.zt.location.modules.business.message.TaskData;

@Component
public class InitData implements CommandLineRunner{

	@Autowired
	private RfidinfoService rfidinfoService;
	
	@Autowired
	private RealTimeService realTimeService;
	
	@Autowired
	private LimitspeedService limitSpeedService;
	
	@Override
	public void run(String... arg0) throws Exception { 
		Map<String, Object> map = new HashMap<String, Object>();
		List<RfidinfoEntity> rfidInfo = rfidinfoService.queryList(map); 
		if(null!=rfidInfo){
			for (RfidinfoEntity rfidinfoEntity : rfidInfo) {
				SysCache.addObject("rfid_"+rfidinfoEntity.getRfidno(), rfidinfoEntity); 
			} 
		}
		
		//初始化所有机器
		List<RealTimeEntity> realTimeEntityList = realTimeService.queryList(null);
		for(RealTimeEntity realTimeEntity:realTimeEntityList){
			TaskData taskData = new TaskData();
			taskData.setTagCode(realTimeEntity.getTagCode());
			taskData.setTitleNo(realTimeEntity.getPos());
			taskData.setReaderID(realTimeEntity.getReaderId());
			taskData.setDirection(realTimeEntity.getDirection());
			taskData.setReaderIP(realTimeEntity.getReaderIp());
			if(null!=realTimeEntity.getUpdateTime() && !"".equals(realTimeEntity.getUpdateTime())){
				try {
					SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
					long time = simpleDateFormat.parse(realTimeEntity.getUpdateTime().replace(".0","")).getTime(); 
					taskData.setAddTime(String.valueOf(time));
				} catch (Exception e) { 
				}
				
			} 
			taskData.setStatus(Integer.parseInt(realTimeEntity.getStatus()));
			RfidinfoEntity temp = (RfidinfoEntity)SysCache.getObject("rfid_"+realTimeEntity.getTagCode());
			taskData.setCurrentState(temp.getAxial());
			SysCache.addMachineObject(realTimeEntity.getReaderIp(), taskData);
		}
		
		
		List<LimitspeedEntity> limitSpeed = limitSpeedService.queryList(null);
		SysCache.setLimitSpeed(limitSpeed); 
		
	}

}
